package com.loves.admin.controller;

import com.loves.admin.service.AccountService;
import com.loves.admin.dto.RegisterDTO;
import com.loves.admin.util.result.ResultApi;
import com.loves.admin.vo.RegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/register")
@Api(value = "注册接口")
public class RegisterController {

    @Resource
    private AccountService accountService;

    @ApiOperation(value = "邮箱发送验证", notes = "根据邮箱发送验证码")
    @PostMapping("/mailVerification")
    public ResultApi mailVerification(@NotEmpty(message = "邮箱名不能为空！") @Email(message = "请输入正确的邮箱地址") @RequestParam String mailName) {
        if (accountService.existMailName(mailName) > 0) {
            return ResultApi.error("该邮箱已注册！请直接登录或者换邮箱注册！");
        }
        accountService.mailVerification(mailName);
        return ResultApi.success("发送成功！");
    }

    @ApiOperation(value = "注册", notes = "根据邮箱名，密码和验证码注册")
    @PostMapping("/registerAccount")
    public ResultApi register(@NotNull(message = "数据不能为空！") @RequestBody @Valid RegisterDTO account) {
        return ResultApi.success(accountService.register(account), "注册成功！");
    }
}
